package com.product.rocketmq;

import com.common.EquityStatus;
import com.common.Message.vo.OfflineMessage;
import com.common.RocketMqConstant;
import com.product.dao.ProductEquityDao;
import com.product.entity.ProductEquity;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @program: Member_Perks_Store
 * @description:
 * @author: 无恙
 * @create: 2024-06-18 18:15
 **/

@Slf4j
@Component
@RocketMQMessageListener(
        topic = RocketMqConstant.PRODUCT_OFFLINE_GROUP,
        consumerGroup = RocketMqConstant.PRODUCT_OFFLINE_GROUP,
        selectorExpression = RocketMqConstant.PRODUCT_OFFLINE_TAG,
        // 指定消费者线程数，默认64，生产中请注意配置，避免过大或者过小
        consumeThreadMax = 5
)
public class OfflineListener extends BaseMqMessageListener<OfflineMessage>
        implements RocketMQListener<OfflineMessage> {

    @Autowired
    ProductEquityDao productEquityDao;

    @Override
    protected String consumerName() {
        return RocketMqConstant.PRODUCT_OFFLINE_GROUP;
    }

    @Override
    protected void handleMessage(OfflineMessage message) throws Exception {
        //商品下线
        ProductEquity productEquity = productEquityDao.selectById(message.getEquityId());

        productEquity.setProductState(EquityStatus.OFFLINE.getCode());

        productEquityDao.updateById(productEquity);

    }

    @Override
    protected void overMaxRetryTimesMessage(OfflineMessage message) {

    }

    @Override
    protected boolean isRetry() {
        return false;
    }

    @Override
    protected boolean isThrowException() {
        return false;
    }

    @Override
    public void onMessage(OfflineMessage productOfflineMessage) {
        super.dispatchMessage(productOfflineMessage);

    }
}
